﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Sharing
open Microsoft.SharePoint.Client.Utilities
open Microsoft.SharePoint.Client.WebParts
open Microsoft.SharePoint.Client.Application
open System.Security;

// =======================================================
// Work with webparts
// =======================================================

let web = Client.Context.Web

Client.Load web

let spFile1 = web.GetFileByServerRelativeUrl("/default.aspx")
let wpManager1 = spFile1.GetLimitedWebPartManager(PersonalizationScope.Shared)
let wps1 = wpManager1.WebParts
Client.Load wps1
wps1.Count

for wp in wps1 do
    Client.Load wp.WebPart
    let msg = wp.WebPart.Title
    printfn "%s\n" msg
    Client.ExecuteQuery()

let mutable wpXML = System.IO.File.ReadAllText("C:\Users\me\Downloads\Documents.webpart")

let wpDef = wpManager1.ImportWebPart(wpXML);
wpManager1.AddWebPart(wpDef.WebPart, "Top_Left", 0)
Client.ExecuteQuery()